﻿using ShopEnzi.Data.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Linq;
using NH.Beng.Repository;
using ShopEnzi.Data.Enums;

namespace ShopEnzi.Web.Utils
{
    public static class ProductHelper
    {
        public static List<Product> GetProducts(this products source)
        {
            var returnList = new List<Product>();
            foreach (var productItem in source.Items)
            {
                returnList.Add(productItem.GetProductEntity());
            }
            return returnList;
        }

        public static Product GetProductEntity(this productsProduct source)
        {
            Product productEntity =null;
            using (var uow = NH.Beng.Repository.UnitOfWork.GetUnitOfWork())
            {
                var productRepo = Product.GetRepository(uow);
                productEntity = productRepo.Linq().FirstOrDefault(q => q.FirmProductId == source.UniqueId);

                if (productEntity == null)
                {
                    productEntity = new Product();
                    productEntity.City = source.city;
                    productEntity.Description1 = source.description1;
                    productEntity.Description2 = source.description2;
                    productEntity.Description3 = source.description3;
                    productEntity.FirmProductId = source.UniqueId;
                    productEntity.ModelName = source.model_name;
                    productEntity.ProductUrl = source.product_url;
                    productEntity.ShortTitle = source.short_title;
                    productEntity.Title = source.title;
                    productEntity.Firm = GetFirm(source.provider_name, uow);
                    productEntity.Images.Add(new ProductImage() { Path = source.image });
                    productEntity.Prices.Add(GetPrice(source));
                    productEntity.Categories = GetCategories(source, uow);
                }
                productRepo.Save(productEntity);
            }
            return productEntity;
        }

        internal static List<Category> GetCategories(productsProduct productsProduct, UnitOfWork uow)
        {
            var categories = new List<Category>();

            if (!string.IsNullOrEmpty(productsProduct.brand_name))
                categories.Add(GetCategory(productsProduct.brand_name, CategoryType.Brand, uow));

            if (!string.IsNullOrEmpty(productsProduct.merchant_category))
                categories.Add(GetCategory(productsProduct.merchant_category, CategoryType.Firm, uow));

            if (!string.IsNullOrEmpty(productsProduct.category1))
                categories.Add(GetCategory(productsProduct.category1, CategoryType.Firm, uow));

            if (!string.IsNullOrEmpty(productsProduct.category2))
                categories.Add(GetCategory(productsProduct.category2, CategoryType.Firm, uow));

            if (!string.IsNullOrEmpty(productsProduct.category3))
                categories.Add(GetCategory(productsProduct.category3, CategoryType.Firm, uow));

            if (!string.IsNullOrEmpty(productsProduct.city))
                categories.Add(GetCategory(productsProduct.city, CategoryType.City, uow));

            if (!string.IsNullOrEmpty(productsProduct.gender))
                categories.Add(GetCategory(productsProduct.gender, CategoryType.Gender, uow));

            return categories.GroupBy(q => q.Id).Select(q => q.First()).ToList();
        }

        internal static Category GetCategory(string name, CategoryType type, UnitOfWork uow)
        {
            var category = Category.GetCategoryByName(name, type, uow);
            if (category == null)
            {
                var categoryRepo = Category.GetRepository(uow);
                category = new Category();
                category.Name = name;
                category.Type = type;
                categoryRepo.Save(category);
            }
            return category;
        }

        internal static ProductPrice GetPrice(productsProduct productsProduct)
        {
            var priceEntity = new ProductPrice();
            decimal price = 0;
            decimal dealPrice = 0;
            decimal.TryParse(productsProduct.price, out price);
            decimal.TryParse(productsProduct.deal_price, out dealPrice);

            if (dealPrice > 0)
            {
                priceEntity.Price = dealPrice;
                priceEntity.OldPrice = price;
            }
            else
                priceEntity.Price = price;

            return priceEntity;
        }

        internal static Firm GetFirm(string name, UnitOfWork uow)
        {
            var firmRepo = Firm.GetRepository(uow);
            var firmEntity = firmRepo.Linq().FirstOrDefault(q => q.Name == name);
            if (firmEntity == null)
            {
                firmEntity = new Firm();
                firmEntity.Name = name;
                firmRepo.Save(firmEntity);
            }
            return firmEntity;
        }
    }
}